package cn.itcast.test;

import lombok.extern.slf4j.Slf4j;

/**
 * interrupt打断线程的使用，会抛出InterruptedException
 * isInterrupted线程正常运行的被打断，线程立即停止，查看isInterrupted值为false
 * isInterrupted线程sleep wait join被打断,调用InterruptedException，线程不会立即停止    ，查看isInterrupted值为false
 */
@Slf4j(topic = "c.InterruptedTest11")
public class InterruptedTest11 {

    public static void main(String[] args) throws InterruptedException {
        test1();
        test2();
    }

    public static void test1() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("输出");
        },"t1");

        t1.start();
        log.debug("interrupt");
        t1.interrupt();
        log.debug("t1是否生存{}", t1.isAlive());
        log.debug("打断标记t1:{}", t1.isInterrupted());
    }

    public static void test2() throws InterruptedException {
        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                log.debug("t2是否生存{}", Thread.currentThread().isAlive());
                e.printStackTrace();
                System.out.println(Thread.currentThread().isInterrupted());
            } finally {
                log.debug("finallyt2是否生存{}", Thread.currentThread().isAlive());
            }

        },"t2");

        t2.start();
        Thread.sleep(1000);
        log.debug("interrupt");
        t2.interrupt();
        log.debug("打断标记t2:{}", t2.isInterrupted());
    }
}
